package com.chw.service.impl;

import com.chw.common.ChwData;
import com.chw.common.exception.NameException;
import com.chw.dao.IUserDao;
import com.chw.model.User;
import com.chw.model.vo.UserVo;
import com.chw.service.IUserService;
import com.chw.utils.ChwMD5;
import com.chw.utils.email.ToEmail;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {
    @Resource
    private IUserDao userDao;
    public Object area(Integer pid) {

        return userDao.area(pid);
    }

    public Object addUser(User user) throws MessagingException, UnsupportedEncodingException {
        if (user.getName()==null){
            throw  new NameException("用户名不能为空");
        }
        User user1=userDao.getName(user.getName());
        if (user1!=null){
           return 0;
        }
        String s = ChwMD5.chwMD5(ChwMD5.chwMD5(user.getPassword()) + user.getName());
        user.setPassword(s);
        user.setCreateDate(new Date());
        userDao.addUser(user);
        User name = userDao.getName(user.getName());
        ToEmail.sendMail(name.getEmail(),"用户"+name.getName()+"你好，您是第"+name.getId()+"位注册用户","悦薇花店");
        return 1;
    }

    public Object isExit(String name ,String password) {
        /*User user=userDao.getName(name);
        if (user==null){
            return 0;
        }
        String s = ChwMD5.chwMD5(ChwMD5.chwMD5(password) + name);
        if (user.getPassword().equals(s)){
            return 1;
        }
        return 2;*/
        int status=1;
        Subject subject= SecurityUtils.getSubject();
        //密码加密，保持一致
        String ps=ChwMD5.chwMD5(ChwMD5.chwMD5(password)+name);
        //信息认证
        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(name,ps);
        try{
            subject.login(usernamePasswordToken);
        }catch (UnknownAccountException accountException){
            //用户名错误
            status=2;
        }catch (IncorrectCredentialsException exception){
            //密码错误
            status=3;

        }catch (Exception e){
            //程序异常
            status=-1;
        }
        return status;
    }

    public Object queryUser(UserVo userVo) {
        userVo.setPage((userVo.getPage()-1)*userVo.getLimit());
        Integer count =userDao.count(userVo);
        List<UserVo> userVos = userDao.queryUser(userVo);
        Map map=new HashMap();
        map.put("count",count);
        map.put("msg","success");
        map.put("data",userVos);
        map.put("code",0);
        return map;
    }

    public void upUser(User user) {
        userDao.upUser(user);
    }

    public Object upPass(String name, Integer id, String password, String password1) {
        User name1 = userDao.getName(name);
        String s = ChwMD5.chwMD5(ChwMD5.chwMD5(password) + name);
        if (name1==null){
            return 0;
        }
        if (name1.getPassword().equals(s)){
            String s1 = ChwMD5.chwMD5(ChwMD5.chwMD5(password1) + name);
            userDao.upPass(id,s1);
            return 1;
        }
        return 2;
    }
}
